package com.hnmqet.demo01.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnmqet.demo01.dao.AccountTblMapper;
import com.hnmqet.demo01.entity.AccountTbl;
import com.hnmqet.demo01.service.AccountTblService;
import io.seata.spring.annotation.GlobalLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author td
 * @since 2020-01-02
 */
@RestController
@RequestMapping
public class AccountTblController {
    @Autowired
    AccountTblService accountTblService;
    @Autowired
    AccountTblMapper accountTblMapper;

    @GetMapping("/test")
    public Object test() throws InterruptedException {
        accountTblService.test();
        return "执行完毕！";
    }

    @GetMapping("/test001")
    public Object test001() {
        Page page = new Page();
        page.setCurrent(1);
        page.setSize(10);
        Page page2 = accountTblMapper.selectPage(page, null);
        return page2;
    }

    @GetMapping("/findOne")
    public Object findOne() {
        AccountTbl one = accountTblMapper.findOne();


        //  批量插入测试

        List<AccountTbl> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            AccountTbl accountTbl = new AccountTbl();
            accountTbl.setUserId("dsadasda" + System.nanoTime());
            accountTbl.setMoney(100 + i);
            list.add(accountTbl);
        }

        accountTblService.insetMore(list);
        return one;
    }

    @GetMapping("/batchUpdate")
    public Object batchUpdate() {
        accountTblService.batchUpdate();

        return "成功执行！！！";
    }

    @GlobalLock(lockRetryInternal = 100, lockRetryTimes = 100)
    @GetMapping("/GlobalLock")
    @Transactional
    public Object GlobalLock() {
        AccountTbl accountTbl = accountTblMapper.selectById(11111111);
        AccountTbl accountTbl1 = accountTbl.setMoney(accountTbl.getMoney() - 1);
        accountTblMapper.updateById(accountTbl1);
        return "成功执行！！！";
    }
}

